/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.ChungTu_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class ChungTu_DAO {
   
     public static String Get_MaCT() throws SQLException{
        String MaCT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "CT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaCT = Rs.getString(1);
        }
        return MaCT;
    }
    
    public static ArrayList<ChungTu_DTO> GetALL()
    {
        ArrayList<ChungTu_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from ChungTu";
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                ChungTu_DTO ChungTu = new ChungTu_DTO();
                ChungTu.setMaCT(rs.getString("MaCT"));
                ChungTu.setTenCT(rs.getString("TenCT"));
                ChungTu.setNgayLap(rs.getDate("NgayLap"));
                ChungTu.setThanhTien(rs.getDouble("ThanhTien"));
                ChungTu.setGhiChu(rs.getString("GhiChu"));
                ds.add(ChungTu);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(ChungTu_DTO ChungTu)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayLap = fmt.format(ChungTu.getNgayLap());
            String sql = "Insert ChungTu Values('"+ChungTu.getMaCT()+"','"+ChungTu.getTenCT()+"','"+NgayLap+"','"+ChungTu.getThanhTien()+"','"+ChungTu.getGhiChu()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(ChungTu_DTO ChungTu)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayLap = fmt.format(ChungTu.getNgayLap());
            String sql = "Update ChungTu Set TenCT = '"+ ChungTu.getTenCT() +"', NgayLap = '"+NgayLap+"', ThanhTien = "+ChungTu.getThanhTien()+", GhiChu = '"+ ChungTu.getGhiChu()+ "' where MaCT = '"+ ChungTu.getMaCT()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maCT)
    {
        boolean flag = false;
        try {
            String sql = "Delete from ChungTu where MaCT = '"+maCT+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
